<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
//宝岛会员体系用表

namespace app\common\model\jooj_member;

use think\Db;
use app\common\model\jsm\Members as JsmMembersModel;
use app\common\model\jsm\Mps as JsmMpsModel;

class Member extends Base {

    protected $table = 'jj_member_1';
    protected $connection = ['database' => 'jooj_member'];
    private $_baodaoAppId = 'wx68f2b24a06465298';

    public function __construct($data = []) {
        parent::__construct($data);
    }

    public function getMemberInfo($params): array {
        try {
            $res = $this->alias('member')->join('jj_member_info_1 info', 'member.vip_id = info.vip_id', 'left')->where($params)->column('member.*,info.Info1 as info1');
        } catch (\Exception $e) {
            return ['status' => false, 'msg' => $e->getMessage()];
        }

        if (!empty($res)) {
            return current($res);
        }

        return [];
    }

    public function registerMember($params): string {

        $vip = 'wxApplet_' . uniqid();
        $date_time = date('Y-m-d H:i:s', time());
        $oldVip = '';
        $memberInfoUpdate = false;
        $memberUpdate = false;

        $JsmMpsModel = new JsmMpsModel();
        $mpId = $JsmMpsModel->getMpsByAppId($this->_baodaoAppId, true)['mp_id'];

        $JsmMembersModel = new JsmMembersModel($mpId);

        if (!$params['mobile']) {
            throw new \Exception('mobile is empty');
        }

        $memberOldInfo = $this->where(['MobileNO' => $params['mobile']])->select();

        if (!empty($memberOldInfo)) {
            $oldVip = $memberOldInfo[0]['vip_id'];
            $memberOldInfoInfo = Db::name('jooj_member.jj_member_info_1')->where(['vip_id' => $oldVip])->select();
            if (!empty($memberOldInfoInfo)) {
                $memberOldInfoInfo = $memberOldInfoInfo[0];
                $memberInfoUpdate = true;
            }
        } else {
            $memberOldInfoInfo = Db::name('jooj_member.jj_member_info_1')->where(['wx_unionid' => $params['wx_unionid']])->select();
            if (!empty($memberOldInfoInfo)) {
                $memberOldInfoInfo = $memberOldInfoInfo[0];
                $oldVip = $memberOldInfoInfo['vip_id'];
                $memberUpdate = true;
                $memberInfoUpdate = true;
            }
        }

        $member = [
            'vip_id' => $vip,
            'data_status' => 10,
            'mobile_status' => 1,
            'come_from' => 7,
            'CreateDate' => $date_time,
            'MobileNO' => $params['mobile'],
        ];

        $openid = $JsmMembersModel->getMembersByCondition(['unionid' => $params['wx_unionid']], 'openid')['openid']; //创建会员时，尝试在粉丝表找openid

        if (($memberInfoUpdate == true) && ($memberOldInfoInfo['Info1']) && ($memberOldInfoInfo['Info1'] != $openid)) {
            throw new \Exception('mobile is binding');
        }

        $memberInfo = [
            'vip_id' => ($oldVip) ? $oldVip : $vip,
            'come_from' => ($oldVip) ? $memberOldInfo['come_from'] : 7,
            'Info1' => ($openid) ? $openid : '',
            'data_status' => 10,
            'wx_unionid' => $params['wx_unionid'],
        ];

        try {
            $this->startTrans();

            if ($memberUpdate) {
                unset($member['vip_id']);
                $this->where(['vip_id' => $oldVip])->update($member);
            } elseif (!$oldVip) {
                $this->insert($member);
            }

            if ($memberInfoUpdate) {
                unset($memberInfo['vip_id']);
                Db::name('jooj_member.jj_member_info_1')->where(['vip_id' => $oldVip])->update($memberInfo);
            } else {
                Db::name('jooj_member.jj_member_info_1')->insert($memberInfo);
            }

            $this->commit();
        } catch (\Exception $ex) {
            $this->rollback();

            throw new \Exception($ex->getMessage());
        }

        return ($oldVip) ? $oldVip : $vip;
    }

    public function registerMemberNoMobile($params){
        
        $vip = 'wxApplet_' . uniqid();
        $date_time = date('Y-m-d H:i:s', time());
        $oldVip = '';
        $memberInfoUpdate = false;
        $memberUpdate = false;

        $JsmMpsModel = new JsmMpsModel();
        $mpId = $JsmMpsModel->getMpsByAppId($this->_baodaoAppId, true)['mp_id'];

        $JsmMembersModel = new JsmMembersModel($mpId);

        $memberOldInfoInfo = Db::name('jooj_member.jj_member_info_1')->where(['wx_unionid' => $params['wx_unionid']])->select();

        if (!empty($memberOldInfoInfo)) {
            throw new \Exception('wx_unionid is existed');
        }

        $member = [
            'vip_id' => $vip,
            'data_status' => 10,
            'mobile_status' => 1,
            'come_from' => 7,
            'CreateDate' => $date_time,
        ];

        $openid = $JsmMembersModel->getMembersByCondition(['unionid' => $params['wx_unionid']], 'openid')['openid']; //创建会员时，尝试在粉丝表找openid

        $memberInfo = [
            'vip_id' => $vip,
            'come_from' => 7,
            'Info1' => ($openid) ? $openid : '',
            'data_status' => 10,
            'wx_unionid' => $params['wx_unionid'],
        ];

        try {
            $this->startTrans();

            $this->insert($member);

            Db::name('jooj_member.jj_member_info_1')->insert($memberInfo);

            $this->commit();
        } catch (\Exception $ex) {
            $this->rollback();

            throw new \Exception($ex->getMessage());
        }

        return $vip;
    }

}
